package com.java.cloud.account.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.java.cloud.account.entity.Account;
import com.java.cloud.account.service.AccountService;
import com.java.common.utils.PageUtils;
import com.java.common.utils.R;


/**
 * 账户表
 *
 * @author jl
 * @since 2020-03-13 12:21:12
 */
@RestController
@RequestMapping("/account")
public class AccountController {
	@Autowired
	private AccountService accountService;

	/**
	 * 扣减余额
	 */
	@PostMapping("/decrease")
	public R decrease(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money) {
		try {
			Thread.sleep(20000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return accountService.decrease(userId, money) == 1 ? R.ok() : R.error("扣减账户余额失败!");
	}

	/**
	 * 分页查询account
	 * 当前页码page、每页显示记录数limit
	 */
	@GetMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = accountService.queryPage(params);

		return R.ok(page);
	}


	/**
	 * 根据主键查询account
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") Long id) {
		Account account = accountService.getById(id);

		return R.ok(account);
	}

	/**
	 * 新增account
	 */
	@PostMapping("/save")
	public R save(@RequestBody Account account) {
		accountService.save(account);

		return R.ok();
	}

	/**
	 * 修改account
	 */
	@PutMapping("/update")
	public R update(@RequestBody Account account) {
		accountService.updateById(account);

		return R.ok();
	}

	/**
	 * 删除account
	 */
	@DeleteMapping("/delete")
	public R delete(@RequestBody Long[] ids) {
		accountService.removeByIds(Arrays.asList(ids));

		return R.ok();
	}

}
